package service

import (
	"Wechat/dao"
	"Wechat/domain"
	"fmt"
	"github.com/fatih/structs"
	"net/http"
	"strings"

	"reflect"
)

// 通过UserMapper调用数据库函数
var usermapper dao.UserMapper

// 自定义UserInfoService类型，用于调用函数
type UserInfoService string

/*// 添加新用户
func (u UserInfoService) AddUser(userID, userPassword, userIdentity, userUnionID string) bool {
	fmt.Println("This is UserInfoService")
	// 通过账号密码查询记录
	if usermapper.InsertPrimaryMessage(userID, userPassword, userIdentity, userUnionID) > 0 {
		return true
	}
	return false
}*/

// 添加新用户
func (u UserInfoService) AddUser(user domain.User) bool {
	if usermapper.InsertPrimaryMessage(user) > 0 {
		return true
	} else {
		return false
	}
}

// 验证帐号密码：返回两个参数，一个判断是否认证成功， 一个返回错误信息
func (u UserInfoService) VerifyUser(userID, userPassword, userUnionID string) (bool, string) {
	var userRes domain.User = domain.User{}
	fmt.Println("userUnionID:", userUnionID)
	userRes = usermapper.SelectByUserIDPassword(userID, userPassword)
	// 通过反射判断结构体是否为空:若为空,则找不到相应的账号密码信息
	if reflect.DeepEqual(userRes, domain.User{}) {
		return false, "Incorrect account or wrong password."
	}
	fmt.Println(userRes.ToString())
	// 如果检查出来的unionID不相同，则说明不是同一个账号
	if !(userRes.UnionID == userUnionID) {
		return false, "UnionID is WRONG!"
	} else {
		return true, http.StatusText(http.StatusOK)
	}
}

/*// 添加用户信息：返回是否插入成功以及错误数据
func (u UserInfoService) AddUserMessage(name, nickName, headPicPath, grade, school, unionID string) (bool, string) {
	// 如果unionid为空，则返回unionid无效
	if reflect.DeepEqual(usermapper.SelectByUnionID(unionID), domain.User{}) {
		return false, "UnionID is invalid!"
		//	如果修改的行数为0，则说明没有修改，修改失败
	} else if usermapper.ModifyByUnionID(name, nickName, headPicPath, grade, school, unionID) == 0 {
		return false, "Modify personal message ERROR!Please do not resubmit."
		//	否则则修改成功，返回成功信息
	} else {
		return true, http.StatusText(http.StatusOK)
	}
}*/

// 添加用户信息：返回是否插入成功以及错误数据
func (u UserInfoService) AddUserMessage(user domain.User) (bool, string) {
	//	获取指定unionid的用户信息
	userTemp := usermapper.SelectByUnionID(user.UnionID)
	//	检查是否unionid的用户
	if reflect.DeepEqual(userTemp, domain.User{}) {
		return false, "UnionID is invalid"
	}
	//	检查哪些数据需要更新，如果传进来的数据为""，则说明不用更新
	if user.Name == "" {
		user.Name = userTemp.Name
	}
	if user.NickName == "" {
		user.NickName = userTemp.NickName
	}
	if user.HeadPicPath == "" {
		user.HeadPicPath = userTemp.HeadPicPath
	}
	if user.Grade == "" {
		user.Grade = userTemp.Grade
	}
	if user.School == "" {
		user.School = userTemp.School
	}
	//	如果修改的行数为0则说明无需修改
	if usermapper.ModifyByUnionID(user) == 0 {
		return false, "Don`t need to modify your message!"
	} else {
		return true, ""
	}
}

// 获取用户信息
func (u UserInfoService) GetUserMessage(unionID string) (bool, map[string]interface{}) {
	user := usermapper.SelectByUnionID(unionID)
	// 利用structs.Map将结构体转化为Map
	if reflect.DeepEqual(user, domain.User{}) {
		return false, nil
	} else {
		return true, structs.Map(&user)
	}
}

// 修改用户密码
func (u UserInfoService) ModifyUserPassword(unionID, userID, oldPassword, newPassword string) (bool, map[string]interface{}) {
	// unionid不对的情况
	if user := usermapper.SelectByUnionID(unionID); reflect.DeepEqual(user, domain.User{}) {
		return false, map[string]interface{}{"ERROR": "unionid is invalid!"}
		//	账号和旧的密码不对应
	} else if user = usermapper.SelectByUserIDPassword(userID, oldPassword); reflect.DeepEqual(user, domain.User{}) {
		return false, map[string]interface{}{"ERROR": "Incorrect account or wrong password."}
		//	修改失败
	} else if rowsAffected := usermapper.ModifyPassword(unionID, newPassword); rowsAffected == 0 {
		return true, map[string]interface{}{"ERROR": "Modify password Fail"}
		//	修改成功
	} else if rowsAffected == -1 {
		return false, map[string]interface{}{"ERROR": "Old and new passwords are consistent! Please try again!"}
	} else {
		return true, map[string]interface{}{}
	}
}

// AddUserActivity 添加用户报名活动信息
func (u UserInfoService) AddUserActivity(unionID, ActivityID string) (bool, string) {
	// 查询unionID在不在
	user := usermapper.SelectByUnionID(unionID)
	if reflect.DeepEqual(user, domain.User{}) {
		return false, "unionid is invalid!"
	}
	fmt.Println("UnionID: ", unionID, "ActivityID", ActivityID)
	//	 将新的活动编号和旧的活动编号拼接起来
	if user.ActivityID == "" {
		user.ActivityID = ActivityID
	} else {
		user.ActivityID += "|"
		user.ActivityID += ActivityID
	}
	//	 通过修改用户信息，插入新的user
	if usermapper.ModifyByUnionID(user) == 0 {
		return false, "Don`t need to modify!"
	} else {
		return true, ""
	}
}

// 获取用户参加活动信息，并且用列表返回活动编号
func (u UserInfoService) GetUserActivityList(unionID string) (bool, []string) {
	//	查询unionID在不在
	user := usermapper.SelectByUnionID(unionID)
	if reflect.DeepEqual(user, domain.User{}) {
		return false, nil
	}
	//	 返回Activity的切片
	ActivityArr := strings.Split(user.ActivityID, "|")
	return true, ActivityArr
}
